<h2>EZproxy Record</h2>
<?php
$id = rv('id');
if (!$id)
    die('no id?');
if ($id == -1) {
    $ruleset = array(
        'title' => '',
        'url' => '',
        'config' => '',
        'hosts' => array(),
        'domains' => array(),
        'comment' => '',
        'extra' => array(),
        'active' => 1,
        'autologin' => 1,
        'source' => ''
    );
} else {
    $ruleset = get_record($id);
}
if (!$ruleset) {
    echo('<p>No record with ID ' . $id . '</p>');
} else if ($ruleset['source'] == 'SFX') {
    echo('<p>This record is automatically imported from SFX and cannot be edited in this interface.</p>');
} else {
?>
    <form action="" method="post">
        <input type="hidden" name="action" value="update" />
        <input type="hidden" name="what" value="item" />
        <input type="hidden" name="id" value="<?php echo $id; ?>" />
        <label><input class="nodisable" type="checkbox" name="enabled" value="1" <?php if ($ruleset['active'])
        echo 'checked="checked" '; ?>/> Record is enabled</label><br />
        <input class="nodisable" type="hidden" name="id" value="<?php echo $id; ?>" />
        <label>Title: <input id="title" type="text" name="T" size="80" maxlength="255" value="<?php echo $ruleset['title']; ?>" placeholder="[New Item]" /></label><br />
        <label><input type="checkbox" name="autologin" value="1" <?php if (!$ruleset['autologin'])
        echo 'checked="checked" '; ?>/> Always Require Login</label><br />
        <label>Include configuration (URL): <input type="text" id="config" name="C" size="80" maxlength="255" value="<?php echo $ruleset['config']; ?>" onchange="endis(this);" /></label><br />
        <label>URL: <input type="text" name="U" size="80" maxlength="255" value="<?php echo $ruleset['url']; ?>" placeholder="http://" 	/></label><br />
        <label>Comments:<br />
            <textarea name="comments" style="width:100%" rows="5">
<?php echo htmlspecialchars($ruleset['comment']); ?>
            </textarea>
        </label>

    Domain(s):<br />
    <?php
    foreach ($ruleset['domains'] as $d) {
        echo('<input type="text" name="D'
        . ($d[1] ? 'J' : '')
        . '[]" value="' . htmlspecialchars($d[0]) . '" size="80" maxlength="255" /> '
        . 'use JS: <input type="checkbox" onchange="sj(this,\'D\');" '
        . ($d[1] ? 'checked="checked" ' : '') . '/><br />'
        );
    }
    echo('<input type="text" name="D[]" value="" size="80" maxlength="255" /> '
    . 'use JS: <input type="checkbox" onchange="sj(this,\'D\');" /><br />'
    );
    ?>
    <button onclick="return ibb(this,'domain');">Add another</button><br />
    Host(s):<br />
    <?php
    foreach ($ruleset['hosts'] as $d) {
        echo('<input type="text" name="H'
        . ($d[1] ? 'J' : '')
        . '[]" value="' . htmlspecialchars($d[0]) . '" size="80" maxlength="255" /> '
        . 'use JS: <input type="checkbox" onchange="sj(this,\'H\');" '
        . ($d[1] ? 'checked="checked" ' : '') . '/><br />'
        );
    }
    echo('<input type="text" name="H[]" value="" size="80" maxlength="255" /> '
    . 'use JS: <input type="checkbox" onchange="sj(this,\'H\');" /><br />'
    );
    ?>
    <button onclick="return ibb(this,'host');">Add another</button><br />
    <fieldset>
        <legend>Other Parameters (optional)</legend>
        <?php
        foreach ($ruleset['extra'] as $i => $values) {
            echo('Key: <input type="text" name="X[]" value="' . htmlspecialchars($values[0]) . '" size="32" maxlength="255" /> ');
            echo('Value: <input type="text" name="Xv[]" value="' . htmlspecialchars($values[1]) . '" size="80" maxlength="255" /><br />');
        }
        echo('Key: <input type="text" name="X[]" value="" size="32" maxlength="32"/> ');
        echo('Value: <input type="text" name="Xv[]" value="" size="80" maxlength="256"/><br />');
        ?>
        <button onclick="return ibb(this,'extra');">Add another</button>
    </fieldset>
    <input class="nodisable" type="submit" value="Save" />
<?php if ($id > 0) { ?>
    <input class="nodisable" type="submit" name="delete" value="Delete" />
<?php } ?>
    </form>
    <a href="index.php">&laquo; Back</a>
<script type="text/javascript">
    var w;
    var templates={
        'extra':'Key: <input type="text" name="X[]" value="" size="32" maxlength="32" />'
            +' Value: <input type="text" name="Xv[]" value="" size="80" maxlength="256" /><br />',
        'domain':'<input type="text" name="D[]" value="" size="80" maxlength="255" /> use JS: <input type="checkbox" onchange="sj(this,\'D\');" /><br />',
        'host':'<input type="text" name="H[]" value="" size="80" maxlength="255" /> use JS: <input type="checkbox" onchange="sj(this,\'H\');" /><br />'
    };
    function ibb(which,template){
        w=which;
        $(which).prev('br').after(templates[template]);
        return false;
    }
    function sj(elt,l){
        if(elt.checked){
            $(elt).prev('input').attr('name',l+'J[]');
        }else{
            $(elt).prev('input').attr('name',l+'[]');
        }
    }
    function endis(which){
        if(which.value){
            $('input').attr('disabled','disabled');
            $(which).attr('disabled','');
            $('#title').attr('disabled','');
            $('.nodisable').attr('disabled','');
        }else{
            $('input').attr('disabled','');
            $(which).attr('disabled','');
            $('#title').attr('disabled','');
            $('.nodisable').attr('disabled','');
        }
        $('input[type=hidden]').attr('disabled','');
    }
    endis($('#config').get(0));
</script>
<?php
}